package dynamicGui;

import gamePlay.Bet;
import gamePlay.Game;
import gamePlay.Guess;
import gamePlay.Lobby;
import gui.MenuCycling;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import netcode.client.Client;
import netcode.helper.NetcodeHelper;

public class BetMenuE extends JPanel{
	
	Game game_ = MenuCyclingE.game_;
	Client client_ = MenuCyclingE.client_;
	int gameId = MenuCyclingE.game_.getGameID();
	
	
	String name = MenuCyclingE.game_.getPlayers().get(0).getPlayer_name();
	int permaChips = MenuCyclingE.game_.getPlayer().getPermanent_chips();
	
	/*
	 * Begin Constructors---------------------------------------------------
	 */
	//Default Constructor
	//Parameter Lobby is used to get the Map<Integer,Game> allGames;
    public BetMenuE() {
        super();
        System.out.println("Creating BetMenuE");

        
        this.setLayout(new GridLayout(0,2, 0, 10));
        addAllBets();
        addOkButtonToPane();
        addFAQButton();
    }

    /*
	 * End Constructors------------------------------------------------------------------------------------------------
	 */
    
    
    /*Begin button Creation Methods---------------------------------------------------------------------------------
     * 
     */
    //Call this to add in all bets
    private void addAllBets() {
    	List<Guess> guessList = MenuCyclingE.game_.getGuesses();
    	
    	//iterate through all guesses, creating a text field and bet button associated with it
    	Iterator<Guess> iterator = guessList.iterator();
    	int buttonnumber = 0;
    	while (iterator.hasNext()) {
    		buttonnumber++;
    		int guess = iterator.next().getGuess();
    		
    		System.out.println("DEBUG Iterator: " +guess);
    		addBetsToPanel(guess, buttonnumber);
    	}
    }
    //create and add in a betTextFields and buttons
    private void addBetsToPanel(final int answerString, final int buttonNumber){
    	
    	//create a gameName text field for the game name and add it
    	JLabel betTextField = new JLabel(Integer.toString(answerString));
    	betTextField.setPreferredSize(new Dimension(200, 30));
    	this.add(betTextField);
    
        //create the join button and add it
        final JButton betButton = new JButton("Bet on this", null);
        betButton.setPreferredSize(new Dimension(200, 30));
        this.add(betButton);
        betButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	betButtonActionPerformed(evt, buttonNumber, betButton);
            }
        });
    }
    
    private void addOkButtonToPane(){
    	//create the ok button and add it
        JButton betButton = new JButton("Ok");
        betButton.setPreferredSize(new Dimension(200, 30));
        this.add(betButton);
        betButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	okButtonActionPerformed(evt);
            }
        });
    }
    

  //Add FAQButton
  	private void addFAQButton(){
  		JButton refreshButton = new JButton("FAQ");
      	refreshButton.setPreferredSize(new Dimension(200, 30));
      	this.add(refreshButton);
      	refreshButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
              public void actionPerformed(java.awt.event.ActionEvent evt) {
              	FAQButtonActionPerformed(evt);
              }
          });
  	}
  	
    
    /*
     * End button Creation Methods------------------------------------------------------------------------------------------
     */
    
    
    
    
    
    
    
    
    
    
    
    
    
    /*
     *ActionListeners begin--------------------------------------------------------------------------------------------------
     */
 
    //Pressing the join button, it joins a certain game based on the gameCode associated with it
    private void betButtonActionPerformed(java.awt.event.ActionEvent evt, int betButtonNumberin, JButton betButtonIn) {
    	System.out.println("DEBUG: Betting on...#" +betButtonNumberin);
    	
    	
    	
    	boolean validbet = true;
    	int curBet = 0;	//Amount entered for bet
    	String s = (String)JOptionPane.showInputDialog("Please enter the amount of chips you would like to bet");	//User input

    	//Display warning if player enters input which cannot be parsed to an int
    	try{	
    		curBet = Integer.parseInt(s);
    		if (curBet<0)
    		{
    			JOptionPane.showMessageDialog(null, "Invalid Bet");
    			validbet = false;
    		}
    		else if(curBet > MenuCyclingE.game_.getPlayers().get(0).chipsRemaining()){
    			JOptionPane.showMessageDialog(null, "Not enough chips.");
    			validbet = false;
    		}
    	} catch(NumberFormatException number)
    	{
          JOptionPane.showMessageDialog(null, "Invalid Bet");
          validbet = false;
    	}
    	
    	//Place bet if a valid value was entered
    	if (validbet) 	{
//    		game_.getPlayers().get(0).placeBet(betButtonNumberin, curbet);
    		MenuCyclingE.game_.receiveBet(new Bet(name, betButtonNumberin, permaChips, curBet));
    		
    		
    		//change the label on the button
    		betButtonIn.setText("Chips Placed: " + curBet);
    	}
    	else{
			JOptionPane.showMessageDialog(null, "Not enough chips!");
		}
    }
    
	//Pressing the ok button, submit the bets
	private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
	    client_.sendSubmitBets(MenuCyclingE.game_.getBetList(), gameId);
		
		MenuCyclingE.displayResultsMenu();
	}
	
	
	//Pressing FAQ Button
	private void FAQButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QuickGameButtonActionPerformed
		try {
	        //Set your page url in this string. For eg, I m using URL for Google Search engine
	        String url = "http://homepages.uwp.edu/dalby001/faq.html";
	        java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
	      }
	      catch (java.io.IOException e) {
	          System.out.println(e.getMessage());
	      }
	}
	
	
    /*
     *ActionListeners End--------------------------------------------------------------------------------------------------
     */

    

    
    
    
}
